﻿<%@ WebHandler Language="VB" Class="CertifiedHousehold" %>

Imports System
Imports System.Web
Imports System.Data
Imports VSModel
Imports Newtonsoft.Json
Imports System.Xml
Imports System.IO
Imports System.Web.Script.Serialization
Public Class CertifiedHousehold : Implements IHttpHandler
    
    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        Dim json As String
        Dim oOrderId As Integer = 0
        If HttpContext.Current.Request.QueryString("CertifiedID") IsNot Nothing Then
            oOrderId = CType(HttpContext.Current.Request.QueryString("CertifiedID"), Int32)
        End If

        Using data As New VSEntities
            Dim ds As New DataSet("CertifiedHouseholdNganhNghes")
            Dim dt As DataTable = CreateCertifiedHousehold()
            'lấy chuỗi NganhNgheKinhDoanh            
            Dim p = (From o In data.CertifiedHouseholds Where o.CertifiedID = oOrderId Select o.NganhNgheKinhDoanh).FirstOrDefault
            Dim _StrBanDau As String = Nothing
            Dim _StrLayRa As String = Nothing
            Dim _ArrCodeNganhNghe() As string = Nothing
            'mảng dùng xử lý cắt chuổi trong vong lap
            Dim _ArrSplitLanMot() As String = Nothing
            Dim _ArrSplitLanHai() As String = Nothing
            Dim _ArrSplitLanBa() As String = Nothing
            Dim _ArrSplitLanTu() As String = Nothing
            
            If Not p Is Nothing Then
                _StrBanDau = p
                _ArrSplitLanMot = _StrBanDau.Trim().Split("(")
                
            
                For i = 0 To _ArrSplitLanMot.Length - 1
                    _ArrSplitLanHai = _ArrSplitLanMot(i).Trim().Split(")")
                    
                    For j = 0 To _ArrSplitLanHai.Length - 1
                        _ArrSplitLanBa = _ArrSplitLanHai(j).Trim().Split("-")
                        
                        For h = 0 To _ArrSplitLanBa.Length - 1
                            _ArrSplitLanTu = _ArrSplitLanBa(h).Trim().Split("=")
                            
                            For k = 0 To _ArrSplitLanTu.Length - 1
                                If IsNumeric(_ArrSplitLanTu(k)) = True Then
                                    _StrLayRa &= _ArrSplitLanTu(k) & ";"
                            
                                    'trường hợp mảng có số cuối cùng = 0 cắt số 0 thêm vào mảng sử lý code vd:46310 trong danhmucnganhnghe không có ma CAP5 mà có ma CAP4 4631
                                    If (Right(_ArrSplitLanTu(k).Trim(), 1)) = 0 Then
                                        _StrLayRa &= _ArrSplitLanTu(k).Substring(0, _ArrSplitLanTu(k).Length - 1) & ";"
                                    End If
                                End If
                            Next
                        Next
                    Next
                Next
                
                If Not _StrLayRa Is Nothing Then
                    _ArrCodeNganhNghe = _StrLayRa.Trim().Split(";")
                End If
            End If
            
            'lấy Id ngành nghề trường hợp CertifiedHouseholdNganhNghes có chi tiết theo CertifiedID
            Dim _ListIDNganhNghe = (From o In data.CertifiedHouseholdNganhNghes Where o.CertifiedID = oOrderId Select o.DanhMucNganhNgheID).ToList
            'lấy ngành nghề
            If _ListIDNganhNghe.Count = 0 And _ArrCodeNganhNghe Is Nothing Then
                Exit Sub
            End If
            Dim nganhnghe = (From j In data.DanhMucNganhNghes Where _ListIDNganhNghe.Contains(j.Id)  Select j).Distinct().ToList
            
            
            If Not nganhnghe Is Nothing AndAlso nganhnghe.Count > 0 Then
                For Each item In nganhnghe
                    Dim dr As DataRow = dt.NewRow()
                    'biên id ngành nghề
                    Dim IdNganhNghe As Integer = item.Id
                    'lấy Description trong bang CertifiedHouseholdNganhNghes
                    Dim nganhngheDes = (From k In data.CertifiedHouseholdNganhNghes Where k.CertifiedID = oOrderId And k.DanhMucNganhNgheID = IdNganhNghe Select k).FirstOrDefault
                    If Not nganhngheDes Is Nothing Then
                        If nganhngheDes.Description = "" Then
                            dr("Description") = item.Name
                        Else
                            dr("Description") = nganhngheDes.Description
                        End If
                    Else
                        dr("Description") = item.Name
                    End If
                    dr("Code") = item.Code
                    dr("Name") = item.Name
                    dt.Rows.Add(dr)
                Next
                ds.Tables.Add(dt)
            Else
                
                Dim dr As DataRow = dt.NewRow()
                dr("Description") = DBNull.Value
                dr("code") = DBNull.Value
                dr("Name") = DBNull.Value
                dt.Rows.Add(dr)
                ds.Tables.Add(dt)
            End If

            'Finally convert DataSet to JSON format
            json = Ds2Json(ds)

            'Set ContentType return to
            context.Response.ContentType = "application/json; charset=utf-8"
            context.Response.Write(json)
        End Using
    End Sub
    
    Public Shared Function Ds2Json(ByVal ds As DataSet) As String
        Dim sw As New StringWriter()
        ds.WriteXml(sw, XmlWriteMode.IgnoreSchema)
        Dim xd As New XmlDocument()
        xd.LoadXml(sw.ToString())

        Dim jsonText As String = JsonConvert.SerializeXmlNode(xd)
        Return jsonText
    End Function
    
    Public Function CreateCertifiedHousehold() As DataTable
        Dim dt As New DataTable("CertifiedHouseholdNganhNghe")
        Dim dc As New DataColumn()
       
        dc = New DataColumn("Description", System.Type.[GetType]("System.String"))
        dt.Columns.Add(dc)
        dc = New DataColumn("Code", System.Type.[GetType]("System.String"))
        dt.Columns.Add(dc)
        dc = New DataColumn("Name", System.Type.[GetType]("System.String"))
        dt.Columns.Add(dc)
        Return dt
    End Function
    
    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class